package com.ruoyi.job.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.omp.api.RemoteInspectionService;
import com.ruoyi.omp.api.domain.AfElevator;
import com.ruoyi.system.api.RemoteMsgSendService;
import com.ruoyi.system.api.domain.SysMsgAndTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("inspectionElevatorTask")
public class InspectionElevatorTask
{
//    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
//    {
//        System.out.println(StringUtils.format("电梯年检任务执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
//    }

    @Autowired
    private RemoteInspectionService remoteInspectionService;

    @Autowired
    private RemoteMsgSendService remoteMsgSendService;

    @Transactional
    public void ryParams(Integer i)
    {
        //获取电梯列表，check_next_date
        // 日期操作， Date,日期比较
        System.out.println("电梯年检任务：" + i);
        AfElevator seacher = new AfElevator();
        seacher.setExpire(0);
        //是否删除，停用
        R<List<AfElevator>> listAfElevator =  remoteInspectionService.listAfE(seacher);
        List<AfElevator> afElevators = listAfElevator.getData();
        //当前时间
        Date date2 = DateUtil.date();
        for(AfElevator afElevator:afElevators){
//            String dateStr1 = afElevator.getCheckNextDate();
//            Date date1 = DateUtil.parse(dateStr1);
            Date date1 = afElevator.getCheckNextDate();
            Date newDate1 = DateUtil.offset(date1, DateField.MONTH, i);

            //System.out.println(newDate1);
            if(!newDate1.after(date2)){
                // 更新状态
                AfElevator updateAfE = new AfElevator();
                updateAfE.setId(afElevator.getId());
                updateAfE.setExpire(1);
                remoteInspectionService.updateAfElevator(updateAfE);
                //获取消息模板生成消息,第1次获取

                // 插入消息表，发送人
                SysMsgAndTemplate sysMsgSend = new SysMsgAndTemplate();
                sysMsgSend.setModuleType("电梯管理");
                sysMsgSend.getParamList().add(afElevator.getElevatorCode());
                sysMsgSend.getParamList().add(afElevator.getUseFactory());
                //sysMsgSend.setTemplateType(msgTemplate.getTempType());
                //sysMsgSend.setUserId(userId);
                //插入log数据库
                R result = remoteMsgSendService.insertMsg(sysMsgSend);
                System.out.println("检测即将到期");
            }
        }

    }

    /*
    public static void main(String[] args) {
        // 上个月 DateUtil.lastMonth()
        System.out.println(DateUtil.lastMonth());
        String dateStr = "2022-12-01";
        Date date = DateUtil.parse(dateStr,"yyyy-MM-dd");
        System.out.println(date);
        // result 11
        System.out.println(DateUtil.month(date));
        //偏移量,天，月
        Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
        System.out.println(newDate);

        String dateStr1 = "2017-03-01 22:33:23";
        Date date1 = DateUtil.parse(dateStr1);

        String dateStr2 = "2017-04-01 23:33:23";
        Date date2 = DateUtil.parse(dateStr2);

        Date newDate1 = DateUtil.offset(date1, DateField.MONTH, 1);
        System.out.println(newDate1);
        if(newDate1.after(date2)){
            System.out.println("检测即将到期");
        }

        //相差一个月，31天
//        long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
//
//        System.out.println(betweenDay);
    }

    */
//    public void ryNoParams()
//    {
//        System.out.println("电梯年检任务执行无参方法");
//    }
}
